// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2016-2022, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * Driver for LomboTech's nx platform usb2 phy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/syscore_ops.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/clk.h>
#include <mach/common.h>

#include "phy-nx-usb.h"
#include "phy-n7v3-usb.h"
#include "phy-n7v5-usb.h"
#include "phy-n7v7-usb.h"

#define DRIVER_NAME			"lb_nx_usbphy"

/* dts clk reset name */
#define OF_CLK_USB_PHY_RESET		"usb_phy_reset"

#define phy_to_nx_usbphy(p)	container_of(p, struct lb_nx_usbphy, phy)

static const struct of_device_id lb_nx_usbphy_ids[] = {
	{ .compatible = "lombo,n7v3-usbphy", .data = lb_usbphy_get_n7v3_ops, },
	{ .compatible = "lombo,n7v5-usbphy", .data = lb_usbphy_get_n7v5_ops, },
	{ .compatible = "lombo,n7v7-usbphy", .data = lb_usbphy_get_n7v7_ops, },

	{},
};
MODULE_DEVICE_TABLE(of, lb_nx_usbphy_ids);

static int lb_nx_usbphy_soc_suspend(struct lb_nx_usbphy *nx_usbphy,
				    int is_suspend)
{
	int ret = 0;

	if (nx_usbphy->soc_ops->suspend)
		ret = nx_usbphy->soc_ops->suspend(nx_usbphy, is_suspend);

	return ret;
}

static int lb_nx_usbphy_suspend(struct usb_phy *phy, int is_suspend)
{
	struct lb_nx_usbphy *nx_usbphy = phy_to_nx_usbphy(phy);

	return lb_nx_usbphy_soc_suspend(nx_usbphy, is_suspend);
}

static int lb_nx_usbphy_soc_init(struct lb_nx_usbphy *nx_usbphy)
{
	int ret = 0;

	if (nx_usbphy->soc_ops->init)
		ret = nx_usbphy->soc_ops->init(nx_usbphy);

	return ret;
}

static int lb_nx_usbphy_init(struct usb_phy *phy)
{
	struct lb_nx_usbphy *nx_usbphy = phy_to_nx_usbphy(phy);

	return lb_nx_usbphy_soc_init(nx_usbphy);
}

static void lb_nx_usbphy_soc_shutdown(struct lb_nx_usbphy *nx_usbphy)
{
	if (nx_usbphy->soc_ops->shutdown)
		nx_usbphy->soc_ops->shutdown(nx_usbphy);
}

/* shutdown the OTG controller */
static void lb_nx_usbphy_shutdown(struct usb_phy *phy)
{
	struct lb_nx_usbphy *nx_usbphy = phy_to_nx_usbphy(phy);

	return lb_nx_usbphy_soc_shutdown(nx_usbphy);
}

static int lb_nx_usbphy_probe(struct platform_device *pdev)
{
	int				ret;
	struct lb_nx_usbphy		*nx_usbphy;
	const struct of_device_id	*of_id;
	lb_nx_usbphy_get_soc_ops	get_soc_ops;

	dev_info(&pdev->dev, "%s start...\n", __func__);

	of_id = of_match_device(lb_nx_usbphy_ids, &pdev->dev);
	if (!of_id) {
		dev_err(&pdev->dev, "of_match_device failed!\n");
		return -ENODEV;
	}

	if (!of_id->data) {
		dev_err(&pdev->dev, "of data is NULL!\n");
		return -EINVAL;
	}
	get_soc_ops = of_id->data;

	nx_usbphy = devm_kzalloc(&pdev->dev, sizeof(*nx_usbphy), GFP_KERNEL);
	if (!nx_usbphy) {
		dev_err(&pdev->dev, "alloc lb_nx_usbphy structure failed\n");
		return -ENOMEM;
	}

	nx_usbphy->soc_ops = get_soc_ops();
	if (!nx_usbphy->soc_ops) {
		dev_err(&pdev->dev, "usbphy_soc_init failed!\n");
		return -EINVAL;
	}

	nx_usbphy->clk_reset = devm_clk_get(&pdev->dev, OF_CLK_USB_PHY_RESET);
	if (IS_ERR(nx_usbphy->clk_reset)) {
		dev_err(&pdev->dev, "get usb phy reset failed\n");
		return PTR_ERR(nx_usbphy->clk_reset);
	}
	ret = clk_prepare_enable(nx_usbphy->clk_reset);
	if (ret) {
		dev_err(&pdev->dev,
			"%s: enable usb clk reset failed\n", __func__);
		return ret;
	}

	nx_usbphy->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	nx_usbphy->reg_base = devm_ioremap_resource(&pdev->dev, nx_usbphy->res);
	if (IS_ERR(nx_usbphy->reg_base)) {
		dev_err(&pdev->dev, "get ioremap res failed\n");
		ret = PTR_ERR(nx_usbphy->reg_base);
		goto error;
	}

	nx_usbphy->dev			= &pdev->dev;
	nx_usbphy->of_node		= pdev->dev.of_node;
	nx_usbphy->phy.label		= DRIVER_NAME;
	nx_usbphy->phy.dev		= &pdev->dev;
	nx_usbphy->phy.io_priv		= nx_usbphy->reg_base;
	nx_usbphy->phy.init		= lb_nx_usbphy_init;
	nx_usbphy->phy.shutdown		= lb_nx_usbphy_shutdown;
	nx_usbphy->phy.set_suspend	= lb_nx_usbphy_suspend;
	nx_usbphy->phy.type		= USB_PHY_TYPE_USB2;

	platform_set_drvdata(pdev, nx_usbphy);

	ret = usb_add_phy_dev(&nx_usbphy->phy);
	if (ret) {
		dev_err(&pdev->dev, "add usb phy dev failed!\n");
		goto error;
	}

	dev_info(&pdev->dev, "%s done.\n", __func__);

	return 0;

error:
	clk_disable_unprepare(nx_usbphy->clk_reset);

	return ret;
}

static int lb_nx_usbphy_remove(struct platform_device *pdev)
{
	struct lb_nx_usbphy *lb_nx_usbphy = platform_get_drvdata(pdev);

	if (lb_nx_usbphy->clk_reset)
		clk_disable_unprepare(lb_nx_usbphy->clk_reset);

	usb_remove_phy(&lb_nx_usbphy->phy);

	return 0;
}

/* power manager */
#ifdef CONFIG_PM_SLEEP
static int lb_nx_usbphy_module_suspend(struct device *dev)
{
	struct lb_nx_usbphy *nx_usbphy = dev_get_drvdata(dev);

	/*asserted usb phy reset */
	clk_disable_unprepare(nx_usbphy->clk_reset);

	return 0;
}

static int lb_nx_usbphy_module_resume(struct device *dev)
{
	int ret;
	struct lb_nx_usbphy *nx_usbphy = dev_get_drvdata(dev);

	/*de-asserted usb phy reset */
	ret = clk_prepare_enable(nx_usbphy->clk_reset);
	if (ret) {
		dev_err(dev,
			"%s: enable usb phy clk reset failed!\n",
			__func__);
		return ret;
	}

	return 0;
}
#endif /* CONFIG_PM_SLEEP */

static SIMPLE_DEV_PM_OPS(lb_nx_usbphy_pm,
			 lb_nx_usbphy_module_suspend,
			 lb_nx_usbphy_module_resume);

/* driver init and exit */
static struct platform_driver lb_nx_usbphy_driver = {
	.probe = lb_nx_usbphy_probe,
	.remove = lb_nx_usbphy_remove,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
		.of_match_table = lb_nx_usbphy_ids,
		.pm = &lb_nx_usbphy_pm,
	},
};

static int __init lb_nx_usbphy_module_init(void)
{
	return platform_driver_register(&lb_nx_usbphy_driver);
}
subsys_initcall(lb_nx_usbphy_module_init);

static void __exit lb_nx_usbphy_module_exit(void)
{
	platform_driver_unregister(&lb_nx_usbphy_driver);
}
module_exit(lb_nx_usbphy_module_exit);

MODULE_AUTHOR("lomboswer@lombotech.com");
MODULE_DESCRIPTION("Driver for LomboTech's nx platform usb2 phy");
MODULE_LICENSE("GPL");
